package loginProject

import (
	"fmt"
	"os"
)

func UserRegistration() (PersonInfo, string, string) {

	fmt.Println("User Registration module loading...............")

	var (
		userAccount                 string
		userPasswd, userPasswdCheck string
		checkPasswd                 bool  = true
		userId                      int32 = 0
		userName                    string
		userAge                     int8
		userGender                  string
	)

	fmt.Printf("Inter account:")
	_, err := fmt.Scanln(&userAccount)
	for i, _ := range userAccountList {
		if userAccount == i {
			fmt.Printf("Account %s already exist! ", userAccount)
			os.Exit(1)
		}
	}
	if err != nil {
		fmt.Println(err)
	}

	for checkPasswd {
		fmt.Printf("Inter passwd:")
		_, err2 := fmt.Scanln(&userPasswd)
		if err2 != nil {
			fmt.Println(err)
		}

		fmt.Printf("Inter passwd again:")
		_, err3 := fmt.Scanln(&userPasswdCheck)
		if err3 != nil {
			fmt.Println(err)
		}

		if userPasswd == userPasswdCheck {
			checkPasswd = false
		} else {
			fmt.Println("check passwd error！")
			os.Exit(1)
		}
	}

	fmt.Printf("Inter your name:")
	_, err4 := fmt.Scanln(&userName)
	if err4 != nil {
		fmt.Println(err)
	}

	fmt.Printf("Inter your age:")
	_, err5 := fmt.Scanln(&userAge)
	if err5 != nil {
		fmt.Println(err)
	}

	fmt.Printf("Inter your gender:")
	_, err6 := fmt.Scanln(&userGender)
	if err6 != nil {
		fmt.Println(err)
	}

	if userGender == "" {
		userGender = "male"
	}

	userId += 1

	var newPerson PersonInfo = PersonInfo{
		userAccount: userAccount,
		userPasswd:  userPasswd,
		userName:    userName,
		userAge:     userAge,
		userGender:  userGender,
		userId:      userId,
	}

	return newPerson, userAccount, userPasswd

}
